
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IJobTitleBo;
import com.wyzz.hr.dao.IJobTitleDao;
import com.wyzz.hr.pojo.Empsalaryacct;
import com.wyzz.hr.pojo.JobTitle;
import com.wyzz.hr.pojo.Jobgrade;


public class JobTitleBo implements IJobTitleBo {

	public JobTitleBo() {
	}

	public List FindAllJobTitle() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.JobTitle.class);
		dc.setFetchMode("jobtitleDefaultAcct", FetchMode.JOIN);
		dc.setFetchMode("jobtitleDefaultJg", FetchMode.JOIN);
		dc.addOrder(Order.asc("jobtitleSortId"));
		return jobTitleDao.findByCriteria(dc);
	}

	public List FindEnabledJobTitle() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.JobTitle.class);
		dc.setFetchMode("jobtitleDefaultAcct", FetchMode.JOIN);
		dc.setFetchMode("jobtitleDefaultJg", FetchMode.JOIN);
		dc.add(Restrictions.eq(JobTitle.PROP_JOBTITLE_STATUS, new Integer(1)));
		dc.addOrder(Order.asc("jobtitleSortId"));
		return jobTitleDao.findByCriteria(dc);
	}

	public boolean addJobTitle(JobTitle jobTitle) {
		String trimmedName = jobTitle.getJobtitleName().trim();
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.JobTitle.class);
		dc.add(Restrictions.eq(JobTitle.PROP_JOBTITLE_NAME, trimmedName));
		List old = jobTitleDao.findByCriteria(dc);
		if (old.isEmpty()) {
			jobTitle.setJobtitleName(trimmedName);
			if (StringUtils.isNotEmpty(jobTitle.getDefaultAccount()))
				jobTitle.setJobtitleDefaultAcct(new Empsalaryacct(jobTitle
						.getDefaultAccount()));
			else
				jobTitle.setJobtitleDefaultAcct(null);
			if (StringUtils.isNotEmpty(jobTitle.getDefaultJg()))
				jobTitle.setJobtitleDefaultJg(new Jobgrade(jobTitle
						.getDefaultJg()));
			else
				jobTitle.setJobtitleDefaultJg(null);
			jobTitleDao.saveObject(jobTitle);
			return true;
		}
		try {
			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean delJobTitle(Class name, String id) {
		String hql = (new StringBuilder())
				.append(
						"select count(*) from PositionBase where pbJobTitle.jobtitleNo = '")
				.append(id).append("'").toString();
		List old = jobTitleDao.exeHqlList(hql);
		Integer nums = Integer.valueOf(old.get(0).toString());
		if (nums.intValue() > 0)
			return false;
		try {
			jobTitleDao.exeHql((new StringBuilder()).append(
					"delete from JobTitle where jobtitleNo='").append(id)
					.append("'").toString());
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return false;
	}

	public String getJobTitleName(String jobTitleNo) {
		JobTitle jobTitle = (JobTitle) jobTitleDao.loadObject(
				com.wyzz.hr.pojo.JobTitle.class, jobTitleNo, null,
				new boolean[0]);
		return jobTitle != null ? jobTitle.getJobtitleName() : "";
	}

	public List getJobTitlesByNos(String jobTitleNos[]) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.JobTitle.class);
		detachedCriteria.add(Restrictions.in("jobtitleNo", jobTitleNos));
		return jobTitleDao.findByCriteria(detachedCriteria);
	}

	public JobTitle loadJobTitleByNo(String jobTitleNo) {
		return (JobTitle) jobTitleDao.loadObject(
				com.wyzz.hr.pojo.JobTitle.class, jobTitleNo, null,
				new boolean[0]);
	}

	public void saveJobTitleSortIdByBatch(String ids[]) {
		int len = ids.length;
		int sortId = 1;
		for (int i = 0; i < len; i++) {
			jobTitleDao.exeHql((new StringBuilder()).append(
					"update JobTitle set jobtitleSortId=").append(sortId)
					.append(" where jobtitleNo='").append(ids[i]).append("'")
					.toString());
			sortId++;
		}

	}

	public String updateJobTitle(JobTitle jobTitle) {
		if (jobTitle.getJobtitleStatus().intValue() == 0) {
			String hql = (new StringBuilder())
					.append(
							"select count(*) from PositionBase where pbJobTitle.jobtitleNo = '")
					.append(jobTitle.getJobtitleNo()).append(
							"' and pbStatus = '0'").toString();
			List old = jobTitleDao.exeHqlList(hql);
			Integer nums = Integer.valueOf(old.get(0).toString());
			if (nums.intValue() > 0)
				return "1";
		}
		String jobTitleName = jobTitle.getJobtitleName().trim();
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.JobTitle.class);
		dc.add(Restrictions.ne(JobTitle.PROP_ID, jobTitle.getJobtitleNo()));
		dc.add(Restrictions.eq(JobTitle.PROP_JOBTITLE_NAME, jobTitleName));
		List old = jobTitleDao.findByCriteria(dc);
		if (old.isEmpty()) {
			JobTitle oldJobTitle = (JobTitle) jobTitleDao.loadObject(
					com.wyzz.hr.pojo.JobTitle.class, jobTitle.getJobtitleNo(),
					null, new boolean[0]);
			if (StringUtils.isNotEmpty(jobTitle.getDefaultAccount()))
				oldJobTitle.setJobtitleDefaultAcct(new Empsalaryacct(jobTitle
						.getDefaultAccount()));
			else
				oldJobTitle.setJobtitleDefaultAcct(null);
			if (StringUtils.isNotEmpty(jobTitle.getDefaultJg()))
				oldJobTitle.setJobtitleDefaultJg(new Jobgrade(jobTitle
						.getDefaultJg()));
			else
				oldJobTitle.setJobtitleDefaultJg(null);
			oldJobTitle.setJobtitleName(jobTitle.getJobtitleName());
			oldJobTitle.setJobtitleNameDesc(jobTitle.getJobtitleNameDesc());
			oldJobTitle.setJobtitleNeedGmApprove(jobTitle
					.getJobtitleNeedGmApprove());
			oldJobTitle.setJobtitleSortId(jobTitle.getJobtitleSortId());
			oldJobTitle.setJobtitleStatus(jobTitle.getJobtitleStatus());
			jobTitleDao.updateObject(oldJobTitle);
			return "0";
		} else {
			return "2";
		}
	}

	public List loadObjects(Class clazz, String fetch[]) {
		return jobTitleDao.getObjects(clazz, fetch);
	}

	public IJobTitleDao getJobTitleDao() {
		return jobTitleDao;
	}

	public void setJobTitleDao(IJobTitleDao jobTitleDao) {
		this.jobTitleDao = jobTitleDao;
	}

	private IJobTitleDao jobTitleDao;
}
